package com.chenjiaru.io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * 3
 * 转换流InputStreamReader OutputStreamWriter （他可以实现字节流和字符流的转化）
 *      1、使用范围，字节流中的数据都是字符时，转成字符流操作更高效
 */
public class InputStreamReaderDemo {

    public static void main(String[] arr) {
        InputStreamReaderDemo.inputStreamReader("C:\\software\\ideaCode\\chenjiaru\\java\\src\\main\\resources\\a.txt");
    }

    /**
     * 使用InputStreamReader做为中转，实现使用BufferedReader更高效的操作
     * @param src
     */
    public static void inputStreamReader(String src) {
        FileInputStream fileInputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            fileInputStream = new FileInputStream(src);
            inputStreamReader = new InputStreamReader(fileInputStream, "utf-8");    // 这里相当于解码，需要指定字符编码
            bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            while((str = bufferedReader.readLine()) != null) {
                System.out.println(str);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ReaderDemo.close(bufferedReader);
        }

    }

}
